package org.example.classtopia.Service.Impl;

import org.example.classtopia.Mapper.ReportMapper;
import org.example.classtopia.Model.Entity.Report;
import org.example.classtopia.Model.dto.ReportCreateDTO;
import org.example.classtopia.Model.dto.ReportHandleDTO;
import org.example.classtopia.Model.dto.ReportQueryDTO;
import org.example.classtopia.Model.vo.PageResultVO;
import org.example.classtopia.Model.vo.ReportListVO;
import org.example.classtopia.Model.vo.ReportVO;
import org.example.classtopia.Service.ReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Long createReport(ReportCreateDTO reportCreateDTO) {
        // 检查是否重复举报
        int duplicateCount = reportMapper.checkDuplicateReport(
            reportCreateDTO.getReporterId(),
            reportCreateDTO.getTargetType(),
            reportCreateDTO.getTargetId()
        );
        
        if (duplicateCount > 0) {
            throw new RuntimeException("您已经举报过该内容，请勿重复举报");
        }

        Report report = new Report();
        BeanUtils.copyProperties(reportCreateDTO, report);
        report.setCreateTime(LocalDateTime.now());
        report.setStatus("PENDING");

        reportMapper.insertReport(report);
        return report.getId();
    }

    @Override
    public ReportVO getReportById(Long id) {
        Report report = reportMapper.selectReportById(id);
        if (report == null) {
            throw new RuntimeException("举报记录不存在");
        }

        ReportVO reportVO = new ReportVO();
        BeanUtils.copyProperties(report, reportVO);
        return reportVO;
    }

    @Override
    public PageResultVO<ReportListVO> getMyReports(Long reporterId, Integer page, Integer size) {
        int offset = (page - 1) * size;
        
        List<ReportListVO> reports = reportMapper.selectReportsByReporterId(reporterId, offset, size);
        int total = reportMapper.countReportsByReporterId(reporterId);
        
        PageResultVO<ReportListVO> pageResult = new PageResultVO<>();
        pageResult.setRecords(reports);
        pageResult.setTotal((long) total);
        pageResult.setPageNum(page);
        pageResult.setPageSize(size);
        
        return pageResult;
    }

    @Override
    public PageResultVO<ReportListVO> getReportsPage(ReportQueryDTO queryDTO) {
        int offset = (queryDTO.getPage() - 1) * queryDTO.getSize();
        
        List<ReportListVO> reports = reportMapper.selectReportsPage(
            queryDTO.getStatus(),
            queryDTO.getTargetType(),
            offset,
            queryDTO.getSize()
        );
        
        int total = reportMapper.countReports(queryDTO.getStatus(), queryDTO.getTargetType());
        
        PageResultVO<ReportListVO> pageResult = new PageResultVO<>();
        pageResult.setRecords(reports);
        pageResult.setTotal((long) total);
        pageResult.setPageNum(queryDTO.getPage());
        pageResult.setPageSize(queryDTO.getSize());
        
        return pageResult;
    }

    @Override
    public void handleReport(ReportHandleDTO handleDTO) {
        Report report = reportMapper.selectReportById(handleDTO.getId());
        if (report == null) {
            throw new RuntimeException("举报记录不存在");
        }

        if (!"PENDING".equals(report.getStatus())) {
            throw new RuntimeException("该举报已经处理过了");
        }

        report.setStatus(handleDTO.getStatus());
        report.setHandlerId(handleDTO.getHandlerId());
        report.setHandleResult(handleDTO.getHandleResult());
        report.setHandleTime(LocalDateTime.now());

        reportMapper.updateReportHandle(report);
    }
}
